import de.enough.polish.io.RmsStorage;
import java.io.IOException;
import java.util.Vector;

class HiScores
{
	private Vector vector = null;
	private RmsStorage storage;
	
	HiScores()
	{
		try {
			this.storage = new RmsStorage();
			this.vector = (Vector) this.storage.read("TouchSenku");
		} catch(IOException e)
		{
			this.vector = new Vector();
			initialize();
		}
	}
	
	public boolean shouldISaveIt(int mode, int leftCoins, long timeTaken)
	{
		ScoreItem si = (ScoreItem) this.vector.elementAt(getWorst(mode));
		if(si.getMode() == mode)
		{
			if(leftCoins < si.getLeftCoins())
				return true;
			else if(leftCoins == si.getLeftCoins())
			{
				if(timeTaken < si.getTimeTaken())
					return true;
			}
		}
		return false;
	}
	
	public int getWorst(int mode)
	{
		int lf = 100; // infinite
		int id = 0;
		long tt = 1000000L;
		
		for(int j=0; j<10; j++)
		{
			ScoreItem si = (ScoreItem) this.vector.elementAt(j);
			if(si.getMode() == mode)
			{
				if(lf > si.getLeftCoins())
				{
					lf = si.getLeftCoins();
					id = j;
					tt = si.getTimeTaken();
				}
				else if(lf == si.getLeftCoins())
				{
					if(tt > si.getTimeTaken())
					{
						lf = si.getLeftCoins();
						id = j;
						tt = si.getTimeTaken();
					}
				}
			}
		}
		
		return id;
	}
	
	public void restartHiScores()
	{
		this.vector = new Vector();
		try{
			this.storage.save(this.vector, "TouchSenku");
		}catch(IOException e){
		}
	}
	
	private void initialize()
	{
		for(int i=0; i<10; i++)
		{
			add(i/5, "Sam", 30, 0L);
		}
	}
	
	public void add(int mode, String name, int leftCoins, long timeTaken) {
		ScoreItem si = new ScoreItem();
		
		si.setMode(mode);
		si.setName(name);
		si.setLeftCoins(leftCoins);
		si.setTimeTaken(timeTaken);
		
		this.vector.addElement(si);
		
		try{
			this.storage.save(this.vector, "TouchSenku");
		}catch(IOException e){
		}
	}
	
	public ScoreItem get(int place)
	{
		return (ScoreItem) this.vector.elementAt(place);
	}
	
	public void close()
	{
		
	}
	
}